package org.lql.algo.codecrush.hot100.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: liangqinglong
 * @date: 2025-08-31 10:56
 * @description: 131. 分割回文串 <a href="https://leetcode.cn/problems/palindrome-partitioning/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class Partition {

	private String s;
	private final List<String> str = new ArrayList<>();
	private final List<List<String>> ans = new ArrayList<>();

	public List<List<String>> partition(String s) {
		if (s == null || s.isEmpty()) {
			return ans;
		}
		this.s = s;
		recur(0);
		return ans;
	}

	public void recur(int pos) {
		if (pos == s.length()) {
			ans.add(new ArrayList<>(str));
			return;
		}
		for (int i = pos; i < s.length(); i++) {
			if (isPalindrome(pos, i)) {
				str.add(s.substring(pos, i + 1));
				recur(i + 1);
				str.remove(str.size() - 1);
			}
		}

	}

	public boolean isPalindrome(int start, int end) {
		while (start < end) {
			if (s.charAt(start++) != s.charAt(end--)) {
				return false;
			}
		}
		return true;
	}


	public static void main(String[] args) {
		Partition partition = new Partition();
		System.out.println(partition.partition("aab"));
	}
}
